//
//  QuickLoanHouseView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol QuickLoanHouseViewDelegate {
    @objc optional func showHouseSelectView(currentView:LoanScreenView) //选择框弹出
}

class QuickLoanHouseView: UIView {
    
    weak var delegate : QuickLoanHouseViewDelegate?
    
    let houseProvinceView = LoanScreenView.init(textWidth: ScreenW/2) //房产省份
    let houseCityView = LoanScreenView.init(textWidth: ScreenW/2) //房产城市
    let houseTypeView = LoanScreenView.init(textWidth: ScreenW/2) //房产性质
    let houseYearView = LoanScreenView.init(textWidth: ScreenW/2) //房产年代
    let houseAreaView = LoanUnitView.init(textWidth: ScreenW/2) //房产面积
    let housePriceView = LoanUnitView.init(textWidth: ScreenW/2) //房产价格
    let houseGaveView = LoanSelectView.init(textWidth: ScreenW/2) //房产房本
    
    var model = [String:String]() //房产相关数据

    init() {
        super.init(frame: .zero)
        
        houseProvinceView.tag = 1
        houseProvinceView.delegate = self
        houseProvinceView.title = houseProvince
        houseProvinceView.placeholder = selectProvince
        self.addSubview(houseProvinceView)
        
        houseCityView.tag = 2
        houseCityView.delegate = self
        houseCityView.title = houseCity
        houseCityView.placeholder = selectToCity
        self.addSubview(houseCityView)
        
        houseTypeView.tag = 3
        houseTypeView.delegate = self
        houseTypeView.title = houseProperties
        houseTypeView.placeholder = selectHouseProperties
        self.addSubview(houseTypeView)
        
        houseYearView.tag = 4
        houseYearView.delegate = self
        houseYearView.title = buildYear
        houseYearView.placeholder = selectBuildYear
        self.addSubview(houseYearView)
        
        houseAreaView.title = buildArea
        houseAreaView.unitText = squareMeters
        houseAreaView.placeholder = selectBuildArea
        houseAreaView.textLabel.keyboardType = .decimalPad
        houseAreaView.tag = 1
        houseAreaView.delegate = self
        self.addSubview(houseAreaView)
        
        housePriceView.title = housePrice
        housePriceView.unitText = millionYuan
        housePriceView.placeholder = selectHousePrice
        housePriceView.textLabel.keyboardType = .decimalPad
        housePriceView.tag = 2
        housePriceView.delegate = self
        self.addSubview(housePriceView)
        
        houseGaveView.title = hasGave
        houseGaveView.tag = 1
        houseGaveView.delegate = self
        self.addSubview(houseGaveView)
        
        houseProvinceView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
        
        houseCityView.snp.makeConstraints { (make) in
            make.top.equalTo(houseProvinceView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(houseProvinceView)
        }
        
        houseTypeView.snp.makeConstraints { (make) in
            make.top.equalTo(houseCityView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(houseCityView)
        }
        
        houseYearView.snp.makeConstraints { (make) in
            make.top.equalTo(houseTypeView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(houseTypeView)
        }
        
        houseAreaView.snp.makeConstraints { (make) in
            make.top.equalTo(houseYearView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(houseYearView)
        }
        
        housePriceView.snp.makeConstraints { (make) in
            make.top.equalTo(houseAreaView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(houseAreaView)
        }
        
        houseGaveView.snp.makeConstraints { (make) in
            make.top.equalTo(housePriceView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(housePriceView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension QuickLoanHouseView : LoanScreenViewDelegate,LoanSelectViewDelegate,LoanUnitViewDelegate {
    
    //MARK: 选择贷款标签
    func selectLabelType(currentView: LoanScreenView) {
        
        self.delegate?.showHouseSelectView!(currentView: currentView)
    }
    
    //MARK: 选择是否
    func selectLoanType(currentView: LoanSelectView) {
        
        switch currentView.tag {
        case 1: //是否有房本
            if currentView.isYes == "1" {
                model["house_book"] = yes_title
            }else{
                model["house_book"] = no_title
            }
        default:
            break
        }
    }
    
    //MARK: 获取输入内容
    func getUnitText(currentView: LoanUnitView) {
        
        switch currentView.tag {
        case 1:
            model["house_area"] = houseAreaView.text
        case 2:
            model["house_price"] = housePriceView.text
        default:
            break
        }
    }
}
